<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class User extends CI_Controller {

 function __construct()
  {
    parent::__construct();
  }

 function usersList($username="")
 {
	//die('asshole');
	if($this->checkLogin() == true)
	{
		if($this->session->userdata('level') == 1)
		{
			if($username != "")
			{
				$selectedUser = $this->User_model->get($username);
			}
			else
			{
				$selectedUser = NULL;
			}
			$data['selectedUser'] = $selectedUser;
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['users'] = $this->User_model->get();
			$this->load->view('users/index', $data);
		}
		else
		{
			redirect('home', 'refresh');
		}
	}
	else
	{
		redirect('login', 'refresh');
	}
 }
 
 function saveUser()
 {
	if($this->checkLogin() == true)
	{
		if($this->session->userdata('level') == 1)
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$level = $this->input->post('level');
			$mail = $this->input->post('mail');
			$nama = $this->input->post('nama');
			$userData = array(
					'username' => $username,
					'password' => $password,
					'nama' => $nama,
					'level' => $level,
					'mail' => $mail
					);
			//cek users exist or no
			if($this->User_model->is_user_exist($username) == true)
			{
				//Update
				$this->User_model->update($username, $userData);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$username.'</b> berhasil di ubah!</p>');
				redirect('user/usersList');
			}
			else
			{
				//Add
				$this->User_model->insert($userData);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$username.'</b> berhasil di tambah!</p>');
				redirect('user/usersList');
			}
		}
		else
		{
			redirect('home', 'refresh');
		}
	}
	else
	{
		redirect('login', 'refresh');
	}
 }
 
 function deleteUser($username="", $level="")
 {
	if($this->checkLogin() == true)
	{
		//die($level);
		if($this->session->userdata('level') == 1 AND $level != 1)
		{
			if($username != $this->session->userdata('logged_in'))
			{
				$this->User_model->delete($username);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$username.'</b> berhasil di hapus!</p>');
				redirect('user/usersList');
			}
			else
			{
				$this->session->set_flashdata('msg', '<p class="alert alert-danger"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>Tidak bisa hapus diri sendiri!</b></p>');
				redirect('user/usersList');
			}
		}
		else
		{
			redirect('home', 'refresh');
		}
	}
	else
	{
		redirect('login', 'refresh');
	}
	
 }
 
 
 function checkLogin()
 {
	if($this->session->userdata('logged_in'))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

}
?>